package com.cyj.dream.file.controller.minio;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.constant.pagemodel.ResponseUtil;
import com.cyj.dream.file.model.FileUploadRecord;
import com.cyj.dream.file.service.FileManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: 文件管理
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.file.controller.minio
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-13 15:15
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping(value = "/file/manager", name = "文件管理")
@Api(value = "/file/manager", tags = "文件管理")
public class FilesManagementController {

    @Autowired
    private FileManagementService fileManagementService;

    /**
     * 初始化大文件上传
     *
     * @author ChenYongJia
     * @date 2021-9-13 15:21:40
     */
    @ApiOperation("初始化大文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "md5", value = "文件信息的md5值", dataType = "String", required = true),
            @ApiImplicitParam(name = "chunkCount", value = "分片计数", dataType = "String", required = true),
            @ApiImplicitParam(name = "fileSize", value = "文件大小", dataType = "String", required = true)
    })
    @RequestMapping(value = "/initChunkUpload", name = "初始化大文件上传", method = RequestMethod.POST)
    public Object initChunkUpload(String md5, String chunkCount, String fileSize) {
        log.info("进入 初始化大文件上传 控制器方法，md5：{}，chunkCount：{}，fileSize：{}", md5, chunkCount, fileSize);
        if(StrUtil.isEmpty(md5) || StrUtil.isEmpty(chunkCount) || StrUtil.isEmpty(fileSize)){
            log.error("参数缺失请检查参数后重新提交~");
            return ResponseUtil.error("参数缺失请检查参数后重新提交");
        }
        return fileManagementService.initChunkUpload(md5, chunkCount, fileSize);
    }

    /**
     * 合并文件并返回文件信息
     *
     * @author ChenYongJia
     * @date 2021-9-13 15:21:40
     */
    @ApiOperation("合并文件并返回文件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "md5", value = "文件信息的md5值", dataType = "String", required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String", required = true)
    })
    @RequestMapping(value = "/composeFile", name = "合并文件并返回文件信息", method = RequestMethod.POST)
    public Object composeFile(String md5, String fileName) {
        log.info("进入 合并文件并返回文件信息 控制器方法，md5：{}，fileName：{}", md5, fileName);
        if(StrUtil.isEmpty(md5) || StrUtil.isEmpty(fileName)){
            log.error("参数缺失请检查参数后重新提交~");
            return ResponseUtil.error("参数缺失请检查参数后重新提交");
        }
        return fileManagementService.composeFile(md5, fileName);
    }

    /**
     * 限制上传直接上传
     *
     * @author ChenYongJia
     * @date 2021-9-13 15:21:40
     */
    @ApiOperation("限制上传直接上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件信息", dataType = "MultipartFile", required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String", required = true)
    })
    @RequestMapping(value = "/authUpload", name = "限制上传直接上传", method = RequestMethod.POST)
    public Object authUpload(MultipartFile file, String fileName) {
        log.info("进入 限制上传直接上传 控制器方法，fileSize：{}，fileName：{}", file.getSize(), fileName);
        if(file == null || file.getSize() == 0 || StrUtil.isEmpty(fileName)){
            log.error("参数缺失请检查参数后重新提交~");
            return ResponseUtil.error("参数缺失请检查参数后重新提交");
        }
        return fileManagementService.authUpload(file, fileName);
    }

    /**
     * 放行上传直接上传--如何测试--
     * https://blog.csdn.net/zhaluo_dehezi/article/details/95472423
     *
     * @author ChenYongJia
     * @date 2021-9-13 15:21:40
     */
    @ApiOperation("放行上传直接上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件信息", dataType = "MultipartFile", required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String", required = true)
    })
    @RequestMapping(value = "/allowUpload", name = "放行上传直接上传", method = RequestMethod.POST)
    public Object allowUpload(MultipartFile file, String fileName) {
        log.info("进入 放行上传直接上传--如何测试-- 控制器方法，fileSize：{}，fileName：{}", file.getSize(), fileName);
        if(file == null || file.getSize() == 0 || StrUtil.isEmpty(fileName)){
            log.error("参数缺失请检查参数后重新提交~");
            return ResponseUtil.error("参数缺失请检查参数后重新提交");
        }
        return fileManagementService.allowUpload(file, fileName);
    }

    /**
     * 获取到图片文件的base64
     *
     * @author ChenYongJia
     * @date 2021-9-13 15:21:40
     */
    @ApiOperation("获取到图片文件的base64")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileUploadRecord", value = "文件上传记录对象", dataType = "FileUploadRecord", required = true)
    })
    @RequestMapping(value = "/getPicBase64", name = "获取到图片文件的base64", method = RequestMethod.POST)
    public Object getPicBase64(@RequestBody FileUploadRecord fileUploadRecord) {
        log.info("进入 获取到图片文件的base64 控制器方法，fileUploadRecord：{}，", JSONObject.toJSONString(fileUploadRecord));
        if(fileUploadRecord == null){
            log.error("参数缺失请检查参数后重新提交~");
            return ResponseUtil.error("参数缺失请检查参数后重新提交");
        }
        return fileManagementService.getPicBase64(fileUploadRecord);
    }

}
